﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace ImageView
{
    /// <summary>
    /// Interaction logic for FullImageView.xaml
    /// </summary>
    public partial class FullImageView : UserControl
    {
        private bool _flipx, _flipy;

        public FullImageView()
        {
            InitializeComponent();
        }

        public void LoadImage(string filename)
        {
            BitmapImage bms = new BitmapImage();
            bms.BeginInit();
            bms.UriSource = new Uri(filename);
            bms.EndInit();
            bms.Freeze();

            ImageBrush ib = new ImageBrush(bms);
            ib.Freeze();

            RectImage.Fill = null;
            GC.Collect();
            GC.WaitForPendingFinalizers();
            GC.Collect();
            RectImage.Fill = ib;
            RectImage.Width = bms.PixelWidth;
            RectImage.Height = bms.PixelHeight;
        }

        public void ClearImage()
        {
            if (RectImage.Fill != null)
            {
                RectImage.Fill = null;
                GC.Collect();
                GC.WaitForPendingFinalizers();
                GC.Collect();
            }
        }

        private void SlideZoom_ValueChanged(double newValue)
        {
            if (TransfScale == null) return;
            if (_flipx) TransfScale.ScaleX = SlideZoom.Value * -1;
            else TransfScale.ScaleX = SlideZoom.Value;

            if (_flipy) TransfScale.ScaleY = SlideZoom.Value * -1;
            else TransfScale.ScaleY = SlideZoom.Value;
        }

        public Point Scale
        {
            get { return new Point(TransfScale.ScaleX, TransfScale.ScaleY); }
            set
            {
                if (value.X < 0) _flipx = true;
                else _flipx = false;

                if (value.Y < 0) _flipy = true;
                else _flipy = false;

                TransfScale.ScaleX = value.X;
                TransfScale.ScaleY = value.Y;
            }
        }

        public void FlipX()
        {
            TransfScale.ScaleX *= -1;
            if (TransfScale.ScaleX > 0) _flipx = false;
            else _flipx = true;
        }

        public void FlipY()
        {
            TransfScale.ScaleY *= -1;
            if (TransfScale.ScaleY > 0) _flipy = false;
            else _flipy = true;
        }

        public void Rotate90Cw()
        {
            TransfRot.Angle += 90;
        }

        public void Rotate90CCw()
        {
            TransfRot.Angle -= 90;
        }

        public double RotAngle
        {
            get { return TransfRot.Angle; }
            set { TransfRot.Angle = value; }
        }


    }
}
